package com.tzxs.pay.main.installment;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.tzxs.pay.BR;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityInstallBillBinding;
import com.tzxs.pay.databinding.ItemBillInstallmentBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.bill.detail.CollAndInstallDetailActivity;
import com.tzxs.pay.main.bill.model.CollectionBillBean;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.IHolder;
import com.tzxs.pay.util.StatusBarColorSetter;

import java.util.List;

/**
 * 分期付款 账单
 */
public class InstallmentBillActivity extends BaseActivity<ActivityInstallBillBinding> implements IAdapter.ItemNavigator<CollectionBillBean>, Toolbar.OnMenuItemClickListener {
    private static final String TAG = InstallmentBillActivity.class.getSimpleName();
    private BillViewModel model;
    private int page = 1;//默认从第1页开始

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_install_bill;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, getResources().getColor(R.color.colorWhite));
        model = ViewModelProviders.of(this).get(BillViewModel.class);
        dataBinding.setViewModel(model);
        dataBinding.installBillToolbar.setTitle("");
        setSupportActionBar(dataBinding.installBillToolbar);
        dataBinding.installBillToolbar.setOnMenuItemClickListener(this);
        dataBinding.installBillToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.installBillRec.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.installBillRec.setAdapter(new IAdapter<CollectionBillBean, ItemBillInstallmentBinding>() {
            @Override
            protected int getLayoutId(int viewType) {
                return R.layout.item_bill_installment;
            }

            @Override
            protected void convertListener(@NonNull ItemBillInstallmentBinding binding) {
                super.convertListener(binding);
                binding.setItemNavigator(InstallmentBillActivity.this);
            }


            @Override
            public boolean areItemsTheSame(List<CollectionBillBean> oldData, List<CollectionBillBean> data, int oldItemPosition, int newItemPosition) {
                return oldData.get(oldItemPosition).getOut_trade_no().equals(data.get(newItemPosition).getOut_trade_no());
            }

            @Override
            protected int getDataBRId(int itemViewType) {
                return BR.item;
            }

            @Override
            public void onBindViewHolder(IHolder<ItemBillInstallmentBinding> holder, int position) {
                super.onBindViewHolder(holder, position);
                String payStr = null;
                CollectionBillBean bean = getData().get(position);
                //订单状态
                switch (bean.getPay_status()) {
                    case 1:
                        payStr = "支付成功";
                        holder.binding.collectionItemTradeState.setTextColor(Color.parseColor("#1F78CF"));
                        break;
                    case 2:
                        payStr = "支付失败";
                        holder.binding.collectionItemTradeState.setTextColor(Color.parseColor("#999999"));
                        break;
                    case 3:
                        payStr = "等待支付";
                        holder.binding.collectionItemTradeState.setTextColor(Color.parseColor("#27AA30"));
                        break;
                    case 4:
                        payStr = "结算成功";
                        holder.binding.collectionItemTradeState.setTextColor(Color.parseColor("#ED433F"));
                        break;
                    case 5:
                        payStr = "已退款";
                        holder.binding.collectionItemTradeState.setTextColor(Color.parseColor("#EB9205"));
                        break;
                }
                holder.binding.collectionItemTradeState.setText(payStr);
                //支付方式
                switch (bean.getPay_type()) {
                    case "weixin":
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_wechat);
                        break;
                    case "alipay":
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_alipay);
                        break;
                    default:
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_unionpay);
                        break;
                }
            }
        });
        model.snackbarText.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                if (s != null) {
                    Toast.makeText(InstallmentBillActivity.this, s, Toast.LENGTH_SHORT).show();
                }
            }
        });
        model.getCollectionBills(() -> {
        }, "106", null, page, 10, null, null, null, null);
        //刷新数据
        dataBinding.refresh.setOnRefreshListener(refreshLayout -> {
            //下拉刷新重制page
            page = 1;
            //重新加载数据
            model.getCollectionBills(() -> {
            }, "106", null, page, 10, null, null, null, null);
            //延时1s刷新数据
            refreshLayout.finishRefresh(1000);
        });
        //加载更多
        dataBinding.refresh.setOnLoadMoreListener(refreshLayout -> {
            ++page;
            Log.d(TAG, "viewCreate: 此时的page:" + page);
            model.getCollectionBills(() -> {
            }, "106", null, page, 10, null, null, null, null);
            refreshLayout.finishLoadMore(1000);
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }

    //单击事件
    @Override
    public void itemClick(CollectionBillBean item) {
        Intent intent = new Intent(this, CollAndInstallDetailActivity.class);
        intent.putExtra("orderId", item.getOut_trade_no());
        startActivity(intent);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.toolbar_menu_search) {
            Intent intent = new Intent(this, SearchIBillActivity.class);
            startActivity(intent);
            return true;
        }
        return false;
    }
}